﻿using System.Collections.Generic;
using System.Configuration;
using System.Web.Services;
using System.Xml.Linq;
using System.Linq;

namespace ql_hl
{
    /// <summary>
    /// ql 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class ql : System.Web.Services.WebService
    {
        private bool verifyUser(string LogonNo, string Pwd)
        {
            if (LogonNo == "J_DB" && Pwd == "A123456") return true;
            return false;
        }
        public XElement xmlDeal()
        {
            string url = ConfigurationManager.AppSettings["xmlUrl"];
            if (url == "")
            {
                url = System.AppDomain.CurrentDomain.BaseDirectory + "\\QL.xml";
            }
            XDocument xDoc = XDocument.Load(url); //加载xml文件
            XElement root = xDoc.Root;//获取根节点
            return root;
        }
        [WebMethod]
        public string GetTruckInfo(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid,
                string reserve1 = "", string reserve2 = "")
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("GetTruckInfo")
                                             where (string)el.Attribute("Guid") == QRCodeOrGuid
                                             select el;
                if(item.Count()==0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string AddShipmentDetail(string LogonNo, string Pwd, string TruckType, string FirstOrSecond,string QRCodeOrGuid,
                        string CheNo,string Quantity,string ActQuantity,string FirstWeight,string SecondWeight,string  FirstDate,
                        string SecondDate,string PersonWeight,string WeighbridgeNo,string Weighbridge, string reserve1 = "", string reserve2 = ""
            )
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("AddShipmentDetail")
                                             where (string)el.Attribute("Guid") == QRCodeOrGuid
                                             select el;
                if (item.Count() == 0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string UpdateStatus(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid
                    , string reserve1, string reserve2)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("UpdateStatus")
                                             where (string)el.Attribute("Guid") == QRCodeOrGuid
                                             select el;
                if (item.Count() == 0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }
                else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string CancelTruck(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("CancelTruck")
                                             where (string)el.Attribute("Guid") == QRCodeOrGuid
                                             select el;
                if (item.Count() == 0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string verifyCarNo(string LogonNo,string Pwd,string CarNo,string Jpoint)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("verifyCarNo")
                                             where (string)el.Attribute("CarNo") == CarNo
                                             select el;
                if (item.Count() == 0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }
                else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }
        [WebMethod]
        public string UpdateTruck(string LogonNo,string Pwd ,string TruckType,string QRCodeOrGuid,string CheNo,string Quantity,
            string FirstWeight,string SecondWeight,string FirstDate,string SecondDate,string PersonWeight,string WeighbridgeNo,
            string Weighbridge,string reserve1,string reserve2)
        {
            string res = "";
            if (verifyUser(LogonNo, Pwd))
            {
                XElement root = xmlDeal();//获取根节点
                IEnumerable<XElement> item = from el in root.Elements("UpdateTruck")
                                             where (string)el.Attribute("Guid") == QRCodeOrGuid
                                             select el;
                if (item.Count() == 0)
                {
                    res += "{\"error\":\"没有数据\"}";
                }
                else
                {
                    foreach (XElement xe in item)
                    {
                        res += "{";
                        foreach (XElement xel in xe.Elements())
                        {
                            res += "\"" + xel.Name + "\":" + xel.Value + ";";
                        }
                        res = res.Substring(0, res.Length - 1);
                        res += "}";
                    }
                }
            }
            else
            {
                res += "{\"error\":\"login error\"}";
            }
            return res;
        }

        //[WebMethod]
        //public string Re_GetTruckInfo(string LogonNo, string Pwd, string TruckType, string QRCodeOrGuid,
        //        string reserve1 = "", string reserve2 = "")
        //{
        //    string res = "";
        //    if (verifyUser(LogonNo, Pwd))
        //    {
        //        XElement root = xmlDeal();//获取根节点
        //        IEnumerable<XElement> item = from el in root.Elements("GetTruckInfo")
        //                                     where (string)el.Attribute("Guid") == QRCodeOrGuid
        //                                     select el;
        //        if (item.Count() == 0)
        //        {
                    
        //        }else
        //        {

        //        }
        //        foreach (XElement xe in item)
        //        {
        //            if (xe.Name == "GetTruckInfo" && xe.Attribute("Guid").Value == QRCodeOrGuid)
        //            {
        //                res += "{";
        //                foreach (XElement xel in xe.Elements())
        //                {
        //                    xel.SetAttributeValue("ad","11");
        //                    res += "\"" + xel.Name + "\":" + xel.Value + ";";
        //                }
        //                res = res.Substring(0, res.Length - 1);
        //                res += "}";
        //            }
        //        }
        //    }
        //    else
        //    {
        //        res += "{\"error\":\"login error\"}";
        //    }
        //    return res;
        //}
    }
}
